<?php

namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Comm\CommonController as common;
use App\Http\model\Dictionary;

class Car extends Model
{

	/**
	 * [addBrandModel 添加品牌model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [品牌信息] array $brand
	 * @param    [子品牌信息] array $child
	 * @version  6.2
	 */
	public function addBrandModel($brand ,$child){
		$result = array();
		$brand['type'] = 1;
 		$brand['modifier'] = session('username');
 		$brand['modtime'] = date('Y-m-d H:i:s');
 		$brand['pid'] = 0;
		//添加品牌信息
		$brandId = Db::table('car_brand')
					->insertGetId($brand);
		if ($brandId) {

			//批量添加子品牌
		 	for ($i=0; $i < count($child); $i++) { 
		 		
		 		$data[$i]['name'] = $child[$i];
		 		$data[$i]['logo'] = $brand['logo'];
		 		$data[$i]['type'] = 2;
		 		$data[$i]['modifier'] = session('username');
		 		$data[$i]['modtime'] = date('Y-m-d H:i:s');
		 		$data[$i]['pid'] = $brandId;

		 	}

		 	$addChild = Db::table('car_brand')
						->insert($data);
			if ($addChild) {
				
				$result['code'] = 1000;
				$result['msg'] = '添加成功';
			}else{

				$result['code'] = 1004;
				$result['msg'] = '子品牌添加失败';
			}
		}else{

			$result['code'] = 1004;
			$result['msg'] = '品牌添加失败';
		}

		return json_encode($result);
	}

	/**
	 * [findBrandModel 品牌列表model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [查询条件] $where
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function findBrandModel($where){

		$result = array();

		//查询品牌
		$brandObj = Db::table('car_brand')
						->where($where)
						->orderBy('modtime', 'desc')
						->get();
		$brandArr = json_decode(json_encode($brandObj),true);

		foreach ($brandArr as $key => $value) {
			$brandId[] = $value['id'];
		}

		if ($brandArr) {

			//查询子品牌
			$childObj = Db::table('car_brand')
						->whereIn('pid',$brandId)
						->get();
			$childArr = json_decode(json_encode($childObj),true);	

			//整理数组
			foreach ($brandArr as $key => $value) {
				
				$brandArr[$key]['child'] = '';

				foreach ($childArr as $k => $v) {
					
					if ($value['id'] == $v['pid']) {
						
						$brandArr[$key]['child'] .= ','.$v['name'];
					}

				}
			}
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $brandArr;		

		}else{
			$result['code'] = 1004;
    		$result['msg'] = '没有数据';
		}
		return json_encode($result);
	}

	/**
	 * [deleteBrandModel 删除品牌model，软删除]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [品牌id] arr $brandIdArr
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteBrandModel($brandIdArr){

		$update['isdel'] = 2;

		//删除品牌
		$updateBrand = Db::table('car_brand')
						->whereIn('id', $brandIdArr)
						->update($update);
		if ($updateBrand) {
			//删除该品牌下所有的子品牌
			$updateChild = Db::table('car_brand')
							->whereIn('pid', $brandIdArr)
							->update($update);	
			$result['code'] = 1000;
			$result['msg'] = '删除成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}
		
		return json_encode($result);
	}

	/**
	 * [deleteBrandModel 删除子品牌model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [品牌id] int $brandId
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteChildModel($brandId){
		$update['isdel'] = 2;
		//删除品牌
		$deleteBrand = Db::table('car_brand')
						->where('id', $brandId)
						->update($update);
		if ($deleteBrand) {

			$result['code'] = 1000;
			$result['msg'] = '删除成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}
		
		return json_encode($result);
	}

	/**
	 * [updateBrandModel 修改品牌]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [修改数据] $data
	 * @param    [添加子品牌数据] $addData
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updateBrandModel($data,$addData){

		foreach ($data as $key => $value) {
			$value['modifier'] = session('username');
 			$value['modtime'] = date('Y-m-d H:i:s');
 			$brandId = $value['brandId'];
 			unset($value['brandId']);
		 	$updateBrand = Db::table('car_brand')
						->where('id', $brandId)
						->update($value);
		 } 
		//新增数据
		if ($addData) {
			$add = Db::table('car_brand')
					->insert($addData);
			if ($add) {
				
				$result['code'] = 1000;
				$result['msg'] = '修改成功';
			}else{

				$result['code'] = 1004;
				$result['msg'] = '修改失败';
			}
		}else{

			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}
		return json_encode($result);
	}
	/*
    |--------------------------------------------------------------------------
    | 车系操作model部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addSeriesModel 添加车系model]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [车系数据] arr $series
    * @version  6.2
    */
    public function addSeriesModel($series){

    	$series['modifier'] = session('username');
 		$series['modtime'] = date('Y-m-d H:i:s');
 		$seriesData = Db::table('car_series')
 						->where(['brandid'=>$series['brandid'], 'name'=>$series['name']])
 						->first();
 		if ($seriesData) {

			$result['code'] = 1005;
			$result['msg'] = '该品牌下以存在车系名称';

		}

		$add = Db::table('car_series')
				->insert($series);
		if ($add) {

			$result['code'] = 1000;
			$result['msg'] = '添加成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '添加失败';
		}
		return json_encode($result);
    }

    /**
     * [findSeriesModel 车系列表model]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @param    [查询条件] $where
     * @version  6.2
     * @return   [type]     [description]
     */
    public function findSeriesModel($where){

    	$result = array();
    	$fields = array(
    			'car_series.*',
    			'car_brand.id as childid',
    			'car_brand.name as childname',
    			'car_brand.pid'
    		);
		//查询车系与子品牌
		$seriesObj = Db::table('car_series')
						->where($where)
						->join('car_brand','car_series.brandid','=','car_brand.id')
						->select($fields)
						->orderBy('modtime', 'desc')
						->get();
		$seriesArr = json_decode(json_encode($seriesObj),true);

		if ($seriesArr) {

			//根据子品牌查询品牌
			foreach ($seriesArr as $key => $value) {

				if ($value['pid']) {
					
					$brandObj = Db::table('car_brand')
							->where('id',$value['pid'])
							->select(['id','name'])
							->first();
					$brandArr = json_decode(json_encode($brandObj),true);
					$seriesArr[$key]['brandname'] = $brandArr['name'];
				}else{
					$seriesArr[$key]['brandname'] = '';
				}
			}
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $seriesArr;		

		}else{
			$result['code'] = 1004;
    		$result['msg'] = '没有数据';
		}
		return json_encode($result);
    }

    /**
     * [deleteSeries 删除车系model ，软删除]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function deleteSeriesModel($seriesIdArr){

    	$update['isdel'] = 2;

		//删除车系
		$updateSeries = Db::table('car_series')
						->whereIn('id', $seriesIdArr)
						->update($update);
		if ($updateSeries) {
			
			$result['code'] = 1000;
    		$result['msg'] = '删除成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}
		
		return json_encode($result);
    }

    /**
     * [updateSeries 修改车系]
     * @Author   孙丽男
     * @DateTime 2018-06-08
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function updateSeriesModel($data,$seriesId){

    	$data['modifier'] = session('username');
 		$data['modtime'] = date('Y-m-d H:i:s');
 		$series = Db::table('car_series')
 						->where('id',$seriesId)
 						->update($data);
		if ($series) {

			$result['code'] = 1000;
			$result['msg'] = '修改成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '修改失败';
		}
		return json_encode($result);
    }
	/*
    |--------------------------------------------------------------------------
    | 级别操作部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addLevelModel 级别添加model]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [数据] arr $data
    * @version  6.2
    */
   public function addLevelModel($data){

   		$data['modifier'] = session('username');
 		$data['modtime'] = date('Y-m-d H:i:s');
 		$levelData = Db::table('car_level')
 						->where([['pid',$data['pid']], ['name',$data['name']]])
 						->orwhere([['pid',$data['pid']], ['order',$data['order']]])
 						->first();
 		if ($levelData) {

			$result['code'] = 1005;
			$result['msg'] = '数据已存在，请检查名称与排序';
			return json_encode($result);

		}
		$levelObj = Db::table('car_level')
 						->where('pid',$data['pid'])
 						->select('level')
 						->first();
 		$levelArr = json_decode(json_encode($levelObj),true);

 		if ($levelArr) {
 			$level = $levelArr['level'] + 1;
 		}else{
 			$level = 1;
 		}
 		
 		$data['level'] = $level;
		$add = Db::table('car_level')
				->insert($data);
		if ($add) {

			$result['code'] = 1000;
			$result['msg'] = '添加成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '添加失败';
		}
		return json_encode($result);
   }

   /**
    * [findLevelModel 级别列表]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [查询条件] $where
    * @version  6.2
    * @return   [type]     [description]
    */
   public function findLevelModel($where){

   		$levelObj = Db::table('car_level')
						->where($where)
						->orderBy('modtime', 'desc')
						->get();
		$levelArr = json_decode(json_encode($levelObj),true);

		if ($levelArr) {
			foreach ($levelArr as $key => $value) {
				if ($value['level'] == 1) {
					$levelArr[$key]['is_zhu'] = '是';
				}else{
					$levelArr[$key]['is_zhu'] = '否';
				}
				if ($value['status'] == 1) {
					$levelArr[$key]['statusname'] = '启用';
				}else{
					$levelArr[$key]['statusname'] = '冻结';
				}

			}

			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $levelArr;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '没有数据';
		}
		return json_encode($result);

   }

   /**
    * [setStatusModel 删除/冻结model]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [级别id] arr $levelIdArr
    * @param    [修改数据] arr $update
    * @version  6.2
    */
   public function setStatusModel($levelIdArr,$update){

   		$res = 0;
   		$update['modifier'] = session('username');
 		$update['modtime'] = date('Y-m-d H:i:s');
		foreach ($levelIdArr as $key => $value) {

			$updatelevel = Db::table('car_level')
							->where('id', $value)
							->update($update);
			if ($updatelevel) {
				
				$res++;
			}
		}
		if ($res) {
			
			$result['code'] = 1000;
    		$result['msg'] = '操作成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '操作失败';
		}
		
		return json_encode($result);
   }

   /**
    * [updateLevelModel 修改级别]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [type]
    * @version  6.2
    * @return   [type]     [description]
    */
   public function updateLevelModel($data,$levelId){

   		$data['modifier'] = session('username');
 		$data['modtime'] = date('Y-m-d H:i:s');

 		$levelData = Db::table('car_level')
 						->where([['pid',$data['pid']], ['name',$data['name']],['id','<>',$levelId]])
 						->orwhere([['pid',$data['pid']], ['order',$data['order']],['id','<>',$levelId]])
 						->first();
 		if ($levelData) {

			$result['code'] = 1005;
			$result['msg'] = '数据已存在，请检查名称与排序';
			return json_encode($result);

		}
		$levelObj = Db::table('car_level')
 						->where('pid',$data['pid'])
 						->select('level')
 						->first();
 		$levelArr = json_decode(json_encode($levelObj),true);

 		if ($levelArr) {
 			$level = $levelArr['level'] + 1;
 		}else{
 			$level = 1;
 		}
 		
 		$data['level'] = $level;
   		$updatelevel = Db::table('car_level')
						->where('id', $levelId)
						->update($data);

		if ($updatelevel) {
			
			$result['code'] = 1000;
    		$result['msg'] = '修改成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '修改失败';
		}
		
		return json_encode($result);
   }
    /*
    |--------------------------------------------------------------------------
    | 车型model部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addType 车型添加]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @version  6.2
    */
   public function addTypeModel($data){

   		$data['miscellaneous'] = json_encode($data['miscellaneous']);
   		$data['modifier'] = session('username');
 		$data['modtime'] = date('Y-m-d H:i:s');
   		$type = Db::table('car_type')
						->insert($data);
		if ($type) {
			
			$result['code'] = 1000;
			$result['msg'] = '添加成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}
		return $result;
   }

   /**
    * [findTypeModel 车型列表]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [查询条件] $where
    * @version  6.2
    * @return   [type]     [description]
    */
   public function findTypeModel($where){

   		if (!isset($where[0])) {
   			
   			$fields = array(
   				'car_type.id',
   				'car_type.name',
   				'car_type.modifier',
   				'car_type.modtime',
   				'car_type.maxride',
   				'car_brand.id as childid',
   				'car_brand.pid as brandid',
   				'car_brand.name as childname',
   				'car_series.id as series.id',
   				'car_series.name as seriesname',
   			);
   		}else{
   			$fields = array(
   				'car_type.*',
   				'car_brand.id as childid',
   				'car_brand.pid as brandid',
   				'car_brand.name as childname',
   				'car_series.id as series.id',
   				'car_series.name as seriesname',
   			);

   		}

	   	$typeObj = Db::table('car_type')
				->where($where)
				->join('car_series','car_series.id','=','car_type.sid')
				->join('car_brand','car_brand.id','=','car_type.brandid')
				->select($fields)
				->orderBy('car_type.modtime', 'desc')
				->get();
		$typeArr = json_decode(json_encode($typeObj),true);

		if ($typeArr) {

			//根据子品牌查询品牌
			foreach ($typeArr as $key => $value) {

				if ($value['brandid']) {
					
					$brandObj = Db::table('car_brand')
							->where('id',$value['brandid'])
							->select(['id','name'])
							->first();
					$brandArr = json_decode(json_encode($brandObj),true);
					$typeArr[$key]['brandname'] = $brandArr['name'];
				}else{
					$typeArr[$key]['brandname'] = '';
				}
				//调用字典，将json转化为数组
				if (isset($value['type'])) {
					$zidian = new Dictionary;
        			$typeArr[$key]['type'] = $zidian->dictionary($value['type']);
        			$typeArr[$key]['pic'] = json_decode($value['pic']);
        			$typeArr[$key]['miscellaneous'] = json_decode($value['miscellaneous']);
				}
				
			}

			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $typeArr;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '没有数据';
		}
		return json_encode($result);
   }

    /**
     * [deleteTypeModel 删除车型model ，软删除]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function deleteTypeModel($typeIdArr,$update){

    	$res = 0;

		foreach ($typeIdArr as $key => $value) {
			$updateType = Db::table('car_type')
						->where('id', $value)
						->update($update);
			if ($updateType) {
				$res++;
			}
		}
		if ($res) {
			
			$result['code'] = 1000;
    		$result['msg'] = '操作成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '操作失败';
		}
		
		return json_encode($result);
    }

    /**
     * [updateType 修改车型]
     * @Author   孙丽男
     * @DateTime 2018-06-08
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function updateTypeModel($data ,$typeId){

    	$data['miscellaneous'] = json_encode($data['miscellaneous']);
   		$data['modifier'] = session('username');
 		$data['modtime'] = date('Y-m-d H:i:s');
   		$type = Db::table('car_type')
   					->where('id',$typeId)
					->update($data);
		if ($type) {
			
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}
		return $result;
    }
   /*
    |--------------------------------------------------------------------------
    | 添加、修改下拉选项数据展示部分
    |--------------------------------------------------------------------------
    */
   /**
    * [brand 品牌]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [品牌父级id] int $pid  默认为0
    * @version  6.2
    * @return   [type]     [description]
    */
    public function brandModel($pid = 0){

    	$result = array();

		//查询品牌
		$brandObj = Db::table('car_brand')
						->where('pid',$pid)
						->select(['id','name'])
						->get();
		$brandArr = json_decode(json_encode($brandObj),true);

		if ($brandArr) {

			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $brandArr;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '查询失败';
		}
		return json_encode($result);
    }

    /**
     * [level 级别列表]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function levelModel(){
    	$result = array();

		$levelObj = Db::table('car_level')
						->select(['id','name as value','pid'])
						->get();
		$levelArr = json_decode(json_encode($levelObj),true);

		if ($levelArr) {
			//树状结构数据
			$comm = new Common;
			$data = $comm->createTree($levelArr);
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $data;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '没有数据';
		}
		return json_encode($result);
    }
    /**
     * [series 车系]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @param    [品牌id] int $brandId
     * @version  6.2
     * @return   [type]     [description]
     */
    public function seriesModel($brandId){

    	$result = array();
		$seriesObj = Db::table('car_series')
						->where('brandid',$brandId)
						->select(['id','name'])
						->get();
		$seriesArr = json_decode(json_encode($seriesObj),true);

		if ($seriesArr) {

			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $seriesArr;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '查询失败';
		}
		return json_encode($result);
    }

    /**
     * [type 车型列表]
     * @Author   孙丽男
     * @DateTime 2018-06-08
     * @param    [车系id] int $seriesId
     * @version  6.2
     * @return   [type]     [description]
     */
    public function typeModel($seriesId){
    	$result = array();
		$typeObj = Db::table('car_type')
						->where('sid',$seriesId)
						->select(['id','name'])
						->get();
		$typeArr = json_decode(json_encode($typeObj),true);

		if ($typeArr) {

			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $typeArr;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '没有数据';
		}
		return json_encode($result);
    }
}
